package com.interest.modules.packagebranch.entity;

import com.interest.base.common.BuilderUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangfeng on 16-11-15.
 * <p>
 * e.g., branch name: origin/branch_package_huaxiancenter_1.0.0-1p_2016-08-18-11-54-08-ae7351
 */
public class PackageBranch {
    private static final char UNDER_SCORE = '_';
    private static final char JOINT_CHAR = '-';
    private static final String SUFFIX = "p";

    private Integer id;
    private String prefix;
    private String flavor;
    //    private PackageVersion version;
    private String versionName;
    private int versionCode;

    //    private PackageStamp stamp;
    private String year;
    private String month;
    private String day;
    private String hour;
    private String minute;
    private String second;
    private String code;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getFlavor() {
        return flavor;
    }

    public void setFlavor(String flavor) {
        this.flavor = flavor;
    }


    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    //    public PackageVersion getVersion() {
//        return version;
//    }
//
    public void setVersion(PackageVersion version) {
//        this.version = version;
        setVersionName(version.getVersionName());
        setVersionCode(version.getVersionCode());
    }

    //
//    public PackageStamp getStamp() {
//        return stamp;
//    }
//
    public void setStamp(PackageStamp stamp) {
//        this.stamp = stamp;

        setYear(stamp.getYear());
        setMonth(stamp.getMonth());
        setDay(stamp.getDay());
        setHour(stamp.getHour());
        setMinute(stamp.getMinute());
        setSecond(stamp.getSecond());
        setCode(stamp.getCode());
    }

    public String toString() {
        return prefix + UNDER_SCORE + flavor +
                UNDER_SCORE +
                versionName + JOINT_CHAR + versionCode + SUFFIX +
                UNDER_SCORE +
                year + JOINT_CHAR + month + JOINT_CHAR + day + JOINT_CHAR + hour
                + JOINT_CHAR + minute + JOINT_CHAR + second + JOINT_CHAR + code;
    }

    public static PackageBranch from(String text) {
        String[] segments = BuilderUtil.getUnderSegments(text);
        if (null == segments || segments.length != 5) {
            return null;
        }

        PackageStamp stamp = PackageStamp.from(segments[4]);
        if (null == stamp) {
            return null;
        }

        PackageVersion version = PackageVersion.from(segments[3]);
        if (null == version) {
            return null;
        }

        PackageBranch branch = new PackageBranch();
        List<String> prefixSegments = new ArrayList<>();
        prefixSegments.add(segments[0]);
        prefixSegments.add(segments[1]);
        branch.setPrefix(BuilderUtil.underJoin(prefixSegments));
        branch.setFlavor(segments[2]);
        branch.setVersion(version);
        branch.setStamp(stamp);
        return branch;
    }
}
